package com.example.cloudpracticeplatform.controller;

import com.example.cloudpracticeplatform.common.Response;
import com.example.cloudpracticeplatform.entity.Student;
import com.example.cloudpracticeplatform.entity.Teacher;
import com.example.cloudpracticeplatform.service.StudentService;
import com.example.cloudpracticeplatform.service.TeacherService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@CrossOrigin
public class AuthController {

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;

    @PostMapping("/register/teacher")
    public Response<String> teacherRegister(@RequestBody Teacher teacher) {
        int result = teacherService.register(teacher);
        if (result > 0) {
            return Response.success("Teacher registered successfully");
        } else {
            return Response.error("Failed to register teacher");
        }
    }

    @PostMapping("/login/teacher")
    public Response<Teacher> teacherLogin(@RequestParam String username, @RequestParam String password) {
        Teacher teacher = teacherService.login(username, password);
        if (teacher != null) {
            return Response.success(teacher);
        } else {
            return Response.error("Invalid username or password");
        }
    }

    @PostMapping("/register/student")
    public Response<String> studentRegister(@RequestBody Student student) {
        int result = studentService.register(student);
        if (result > 0) {
            return Response.success("Student registered successfully");
        } else {
            return Response.error("Failed to register student");
        }
    }

    @PostMapping("/login/student")
    public Response<Student> studentLogin(@RequestParam String username, @RequestParam String password) {
        Student student = studentService.login(username, password);
        if (student != null) {
            return Response.success(student);
        } else {
            return Response.error("Invalid username or password");
        }
    }
}